﻿namespace ImageProcessing.Forms
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    /// <summary>
    /// Contains all form's stuff
    /// </summary>
    public partial class ThreSholdBlackWhite : Form
    {
        /// <summary>
        /// original image
        /// </summary>
        private DImage oldImage = new DImage();

        /// <summary>
        /// threshold image
        /// </summary>
        private DImage thresholdImage = new DImage();

        /// <summary>
        /// current type "black", "binary"
        /// </summary>
        string currentType ;

        /// <summary>
        /// Initializes a new instance of the <see cref="ThreSholdBlackWhite"/> class.
        /// </summary>
        public ThreSholdBlackWhite(string type)
        {
            this.InitializeComponent();
            this.currentType = type;
            if (Main.ImageList.Count == 0)
            {
                return;
            }

            foreach (DImage img in Main.ImageList)
            {
                this.CmbImage.Items.Add(img.Name);
            }

            this.CmbImage.SelectedIndex = 0;
            this.oldImage.Height1 = Main.ImageList[this.CmbImage.SelectedIndex].ImageStructArray.GetLength(0);
            this.oldImage.Width1 = Main.ImageList[this.CmbImage.SelectedIndex].ImageStructArray.GetLength(1);
            this.oldImage.ImageStructArray = Operations.CopyImage(Main.ImageList[this.CmbImage.SelectedIndex].ImageStructArray, this.oldImage.Width1, this.oldImage.Height1);
            this.PcbOldImage.Image = Operations.DisplayMatrix(Main.ImageList[this.CmbImage.SelectedIndex].ImageStructArray);

            this.trackBar1.Value = 128;
            this.trackBar2.Visible = false;
            this.trackBar3.Visible = false;
            this.textBox2.Visible = false;
            this.textBox3.Visible = false;
            this.textBox1.Text = this.trackBar1.Value.ToString();
            if (currentType == "black")
            {
                this.thresholdImage.ImageStructArray =PixelOperation.ThreSholdWhiteBlack(this.oldImage.ImageStructArray, Convert.ToByte(this.trackBar1.Value),true,true,true);
                this.PcbNewImage.Image = Operations.DisplayMatrix(this.thresholdImage.ImageStructArray);
            }
            else if (currentType == "binary")
            {
                this.thresholdImage.ImageStructArray = this.oldImage.ImageStructArray = Operations.CopyImage(PixelOperation.ThreSholdBinary(this.oldImage.ImageStructArray, Convert.ToByte(this.trackBar1.Value)), Main.ImageList[this.CmbImage.SelectedIndex].ImageStructArray.GetLength(1), Main.ImageList[this.CmbImage.SelectedIndex].ImageStructArray.GetLength(0));
                this.PcbNewImage.Image = Operations.DisplayMatrix(this.thresholdImage.ImageStructArray);
            }
            else 
            {
                this.thresholdImage.ImageStructArray =  Operations.CopyImage(PixelOperation.ThreSholdWhiteBlack(this.oldImage.ImageStructArray, Convert.ToByte(this.trackBar1.Value), true, true, true), Main.ImageList[this.CmbImage.SelectedIndex].ImageStructArray.GetLength(1), Main.ImageList[this.CmbImage.SelectedIndex].ImageStructArray.GetLength(0));
                this.PcbNewImage.Image = Operations.DisplayMatrix(this.thresholdImage.ImageStructArray);
                this.trackBar2.Visible = true;
                this.trackBar3.Visible = true;
                this.trackBar3.Value = 128;
                this.trackBar2.Value = 128;
                this.textBox2.Visible = true;
                this.textBox3.Visible = true;
                this.textBox2.Text = this.trackBar2.Value.ToString();
                this.textBox3.Text = this.trackBar3.Value.ToString();
            }
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the CmbOmage control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void CmbOmage_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.PcbOldImage.Image = Operations.DisplayMatrix(Main.ImageList[this.CmbImage.SelectedIndex].ImageStructArray);
            this.oldImage.ImageStructArray = Operations.CopyImage(Main.ImageList[this.CmbImage.SelectedIndex].ImageStructArray, Main.ImageList[this.CmbImage.SelectedIndex].ImageStructArray.GetLength(1), Main.ImageList[this.CmbImage.SelectedIndex].ImageStructArray.GetLength(0));
        }

        /// <summary>
        /// Handles the Scroll event of the trackBar1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void TrackBar1_Scroll(object sender, EventArgs e)
        {
            this.textBox1.Text = this.trackBar1.Value.ToString();

            // this.thresholdImage.ImageStructArray = oldImage.ImageStructArray = Operations.CopyImage(PixelOperation.ThreSholdWhiteBlack(oldImage.ImageStructArray, Convert.ToByte(trackBar1.Value)), Main.ImageList[this.CmbImage.SelectedIndex].ImageStructArray.GetLength(1), Main.ImageList[this.CmbImage.SelectedIndex].ImageStructArray.GetLength(0));
            if (currentType == "black")
            {
                this.thresholdImage.ImageStructArray =  PixelOperation.ThreSholdWhiteBlack(this.oldImage.ImageStructArray, Convert.ToByte(this.trackBar1.Value), true, true, true);
                this.PcbNewImage.Image = Operations.DisplayMatrix( PixelOperation.ThreSholdWhiteBlack(this.oldImage.ImageStructArray, Convert.ToByte(this.trackBar1.Value), true, true, true));
            }
            else if (currentType == "binary")
            {
                this.thresholdImage.ImageStructArray = PixelOperation.ThreSholdBinary(oldImage.ImageStructArray, Convert.ToByte(trackBar1.Value));
                this.PcbNewImage.Image = Operations.DisplayMatrix(PixelOperation.ThreSholdBinary(Main.ImageList[this.CmbImage.SelectedIndex].ImageStructArray, Convert.ToByte(this.trackBar1.Value)));
            }
            else
            {
                this.oldImage.ImageStructArray = null;
               // this.thresholdImage.ImageStructArray = this.oldImage.ImageStructArray = PixelOperation.ThreSholdWhiteBlack(this.thresholdImage.ImageStructArray, Convert.ToByte(this.trackBar1.Value), Convert.ToByte(this.trackBar2.Value), Convert.ToByte(this.trackBar3.Value));
                this.PcbNewImage.Image = Operations.DisplayMatrix(PixelOperation.ThreSholdWhiteBlack(Main.ImageList[this.CmbImage.SelectedIndex].ImageStructArray, Convert.ToByte(this.trackBar1.Value), Convert.ToByte(this.trackBar2.Value), Convert.ToByte(this.trackBar3.Value)));
            }
        }

        /// <summary>
        /// Handles the Click event of the BtnCsncel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnCsncel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Handles the Click event of the BtnOk control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnOk_Click(object sender, EventArgs e)
        {
            if (currentType == "black")
            {
                Main.OperationName = "Thresholding";
                Main.ImageList[this.CmbImage.SelectedIndex].UpdateImage(this.thresholdImage.ImageStructArray);
            }
            else if (currentType == "binary")
            {
                Main.OperationName = "Binary Thresholding";
                Main.ImageList[this.CmbImage.SelectedIndex].UpdateImage(this.thresholdImage.ImageStructArray);
            }
            else
            {
                Main.OperationName = "Color Thresholding";
                Main.ImageList[this.CmbImage.SelectedIndex].UpdateImage(this.thresholdImage.ImageStructArray);
            }
            this.Close();

        }

        private void trackBar2_Scroll(object sender, EventArgs e)
        {
            this.textBox2.Text = this.trackBar2.Value.ToString();
          //  this.thresholdImage.ImageStructArray = PixelOperation.ThreSholdWhiteBlack(this.thresholdImage.ImageStructArray, Convert.ToByte(this.trackBar1.Value), Convert.ToByte(this.trackBar2.Value), Convert.ToByte(this.trackBar3.Value));
            this.PcbNewImage.Image = Operations.DisplayMatrix(PixelOperation.ThreSholdWhiteBlack(Main.ImageList[this.CmbImage.SelectedIndex].ImageStructArray, Convert.ToByte(this.trackBar1.Value), Convert.ToByte(this.trackBar2.Value), Convert.ToByte(this.trackBar3.Value)));
        }

        private void trackBar3_Scroll(object sender, EventArgs e)
        {
            this.textBox3.Text = this.trackBar3.Value.ToString();
           // this.thresholdImage.ImageStructArray = PixelOperation.ThreSholdWhiteBlack(this.thresholdImage.ImageStructArray, Convert.ToByte(this.trackBar1.Value), Convert.ToByte(this.trackBar2.Value), Convert.ToByte(this.trackBar3.Value));
            this.PcbNewImage.Image = Operations.DisplayMatrix(PixelOperation.ThreSholdWhiteBlack(Main.ImageList[this.CmbImage.SelectedIndex].ImageStructArray, Convert.ToByte(this.trackBar1.Value), Convert.ToByte(this.trackBar2.Value), Convert.ToByte(this.trackBar3.Value)));
        }
    }
}